﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MillerApp.DataContract.Common;

namespace MillerApp.UserControls
{
    public partial class DataPagerControl : UserControl
    {
        public DataPagerControl()
        {
            InitializeComponent();
        }

        private int _pageSize = 100;
        public int PageSize
        {
            get { return _pageSize; }
             set { _pageSize = value; }
        }

        private int _pageIndex = 0;
        public int PageIndex
        {
            get 
            {
                return _pageIndex; 
            }
             set
             { 
                 _pageIndex = value;
                 this.textBox1.Text = (value + 1).ToString();
             }
        }

        public void IniDataPager(int recordCount)
        {
            //合计[XX]页，共计[XX]条记录
            int pageCount=(recordCount/this.PageSize)+1;
            string calcInfo = "共计" + recordCount.ToString() + "条记录, 每页" + this._pageSize.ToString() + ",合计" + pageCount.ToString() + "页";
            lblCalc.Text = calcInfo;
        }

        public event DelegateMessageHanldler PagerChangeEvent;

        private void btnNext_Click(object sender, EventArgs e)
        {
            this.PageIndex = this._pageIndex + 1;
            if (PagerChangeEvent != null)
            {
                Dictionary<string, object> args = new Dictionary<string, object>();
                args.Add("1", _pageIndex);
                PagerChangeEvent(args);
            }
        }

        private void btnPre_Click(object sender, EventArgs e)
        {
            this.PageIndex = this._pageIndex - 1;
            if (PagerChangeEvent != null)
            {
                Dictionary<string, object> args = new Dictionary<string, object>();
                args.Add("-1", _pageIndex);
                PagerChangeEvent(args);
            }
        }

        private void btnGO_Click(object sender, EventArgs e)
        {
            int page = 0;
            if(int.TryParse(this.textBox1.Text,out page))
            {
                this.PageIndex = page - 1;
            }
            if (PagerChangeEvent != null)
            {
                Dictionary<string, object> args = new Dictionary<string, object>();
                args.Add("0", _pageIndex);
                PagerChangeEvent(args);
            }
        }
    }
}
